//
//  MatchByPlaygroundViewController.m
//  YouthOlympic2014
//
//  Created by xiangpaopao on 14-7-4.
//  Copyright (c) 2014年 mitian. All rights reserved.
//

#import "MatchByPlaygroundViewController.h"
#import "AppDelegate.h"
#import "AlertService.h"
#import "XYAlertViewHeader.h"
#import "GameService.h"

@interface MatchByPlaygroundViewController ()


@property (nonatomic,strong)IBOutlet UILabel *titleLabel;
@property (nonatomic,strong)IBOutlet UILabel *subTitleLabel;
@property (nonatomic,strong)IBOutlet UIImageView *iconView;
@property (nonatomic,strong) UIBarButtonItem *rButtonItem;
@property (nonatomic,strong)Place *place;

@end

@implementation MatchByPlaygroundViewController
@synthesize placeId,subplaceId,queryDateStr, timeline,keys;

-(void) initWithPlace:(Place *)place andSubPlaceId:(NSString *)subpId andDate:(NSString *)date
{
    self.placeId = place.id;
    self.subplaceId = subpId;
    self.queryDateStr = date;
    
    self.place = place;
    
    
}
- (void)viewDidLoad
{
    [super viewDidLoad];
    [self setCtrlBackBtn];
    [self setupRButton];
     self.eventListViewOffset = 65;
    
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    //
    if ([theApp isAppReachable]) {
        
        [self updateMainDataAndViewByDate:self.queryDateStr isFirstLoad:YES];;
        [self updateDateMarks];
    }
    
    
    [self.titleLabel setText:self.place.name];
    [self.subTitleLabel setText:self.place.address];
    [self.iconView setImage:[self.place icon]];
}
-(void)setupRButton{
    UIButton* rButton= [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 90, 30)];
    [rButton setContentHorizontalAlignment:UIControlContentHorizontalAlignmentRight];
    [rButton.titleLabel setFont:[UIFont systemFontOfSize:14]];
    [rButton setTitle:NSLocalizedString(@"全部关注", nil) forState:UIControlStateNormal];
    [rButton addTarget:self action:@selector(followAll:) forControlEvents:UIControlEventTouchUpInside];
    self.rButtonItem = [[UIBarButtonItem alloc] initWithCustomView:rButton];
    
}

-(void)followAll:(id)sender{
    //关注全部
    XYAlertView *alertView = [XYAlertView alertViewWithTitle:NSLocalizedString(@"关注该项目全部赛事", nil)
                                                     message:NSLocalizedString(@"点击全部关注即可将该项目全部赛事日程", nil)
                                                     buttons:[NSArray arrayWithObjects:NSLocalizedString(@"关注", nil), NSLocalizedString(@"取消", nil), nil]
                                                afterDismiss:^(int buttonIndex) {
                                                    if (buttonIndex == 0) {
                                                        NSMutableArray *gameEvents = [[NSMutableArray alloc] init];
                                                        for (NSString *key in self.keys) {
                                                            for (int i=0; i<[self.timeline[key] count]; i++) {
                                                                GameEvent* event = self.timeline[key][i];
                                                                [gameEvents addObject:event];
                                                                [event setStared:YES];
                                                                self.timeline[key][i] = event;
                                                                //添加到提醒
                                                                [[[AlertService alloc] init] addNotificationForEvent:event];
                                                            }
                                                        }
                                                        [[[GameService alloc] init] addGameEventsToSchedule:gameEvents];
                                                        [self.eventListView reloadData];
                                                    }
                                                    
                                                }];
    
    [alertView setButtonStyle:XYButtonStyleGray atIndex:1];
    
    [alertView show];
    
}
- (void)updateMainDataAndViewByDate:(NSString*)date isFirstLoad:(BOOL)first
{
    self.HUD.mode = MBProgressHUDModeIndeterminate;
    self.HUD.labelText = nil;
    [self.HUD show:YES];
    [[[GameService alloc] init] getGameEventsDictByPlaceId:self.placeId
                                             andSubPlaceId:self.subplaceId
                                                  andDate: date
                                               completion:^(id result, NSError *error) {
                                                   
                                                   [self.HUD hide:YES];
                                                   if (error == nil && [result count] != 0) {
                                                       self.timeline = result;
                                                       self.keys = [[self.timeline allKeys] sortedArrayUsingSelector:@selector(compare:)];
                                                       if (![self.subplaceId isEqualToString:@""]) {
                                                           [self.navigationItem setRightBarButtonItem:self.rButtonItem animated:YES];
                                                       }
                                                       
                                                   }else{
                                                       if (![self.subplaceId isEqualToString:@""]) {
                                                           [self.navigationItem setRightBarButtonItem:nil animated:YES];
                                                       }
                                                       self.timeline = nil;
                                                       self.keys = nil;
                                                   }
                                                   [self reloadEventData];
                                                   if (first) {
                                                       [self initSelectedDay];
                                                   }
                                               }];
    //天气
    [[[WeatherService alloc] init] getWeather:date
                                   completion:^(id result, NSError *error) {
                                       if (error == nil) {
                                           [self.weatherLb setText:result];
                                       }
                                   }];
}

-(void)updateDateMarks
{
    self.HUD.mode = MBProgressHUDModeIndeterminate;
    [self.HUD show:YES];
    [[[CalendarMarkService alloc] init] getDaysByPlaceType:self.placeId
                                           andSubPlaceType:self.subplaceId
                                               completion:^(id result, NSError *error) {
                                                   [self.HUD hide:YES];
                                                   
                                                   if (nil == error) {
                                                       self.dateMarks = result;
                                                       [self reloadCalendarData];
                                                       
                                                   }else{
                                                       //TODO
                                                   }
                                               }];
}

@end
